package cn.gdrj.est.service.impl;

import cn.gdrj.est.constants.Constants;
import cn.gdrj.est.dao.EvaluateDao;
import cn.gdrj.est.dto.*;
import cn.gdrj.est.entity.Evaluate;
import cn.gdrj.est.entity.EvaluateDetail;
import cn.gdrj.est.entity.Users;
import cn.gdrj.est.service.*;
import cn.gdrj.est.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * 评估业务组件
 *
 * @author jun
 * @date 2021/11/9
 */
@Service
public class EvaluateServiceImpl implements EvaluateService {
    private static final BigDecimal divisor = BigDecimal.valueOf(100L);
    @Autowired
    private EvaluateDao evaluateDao;
    @Autowired
    private MalfunctionOptionsService malfunctionOptionsService;
    @Autowired
    private MalfunctionService malfunctionService;
    @Autowired
    private EvaluateDetailService evaluateDetailService;
    @Autowired
    private ModelService modelService;

    @Transactional
    @Override
    public EvaluateDTO assess(Long modelId, List<Long> malfunctionOptionsIds, Users users) {
        // 查询故障的选项信息
        List<MalfunctionOptionsDTO> malfunctionOptionsDTOS =
                malfunctionOptionsService.listByIds(malfunctionOptionsIds);

        BigDecimal total = BigDecimal.ZERO;
        for (MalfunctionOptionsDTO optionsDTO : malfunctionOptionsDTOS) {
            // 根据选项查询故障的信息，获取维修的最高价
            Long malfId = optionsDTO.getMalfId();
            MalfunctionDTO malfunctionDTO = malfunctionService.getById(malfId);
            // 小计
            BigDecimal topPrice = malfunctionDTO.getMaintainTopPrice();
            BigDecimal smallTotal = topPrice;
            Integer processType = optionsDTO.getProcessType();
            if(Constants.ProcessType.MAINTAIN.equals(processType)){
                // 因为ratio是一个整数，所以先乘占比值，然后还需要除以 100
                smallTotal = smallTotal.multiply(new BigDecimal(optionsDTO.getRatio()));
                smallTotal = smallTotal.divide(divisor,2, RoundingMode.HALF_UP);
            }
            // 计算评估的总维修价
            total = total.add(smallTotal);
        }

        // 保存评估信息
        Evaluate evaluate = new Evaluate();
        evaluate.setModelId(modelId);
        evaluate.setSubscription(total.multiply(new BigDecimal("0.1")));
        evaluate.setPrice(total);
        evaluate.setRemark("");
        evaluate.setCreatedUserId(users.getId());
        this.saveEvaluate(evaluate);

        // 保存评估的详情信息
        for (MalfunctionOptionsDTO optionsDTO : malfunctionOptionsDTOS) {
            EvaluateDetail evaluateDetail = new EvaluateDetail();
            evaluateDetail.setEvaluateId(evaluate.getId());
            evaluateDetail.setMalfId(optionsDTO.getMalfId());
            evaluateDetail.setOptionId(optionsDTO.getId());
            evaluateDetail.setOptionName(optionsDTO.getOptionName());
            evaluateDetail.setCreatedUserId(users.getId());
            evaluateDetailService.saveEvaluateDetail(evaluateDetail);
        }

        return BeanUtils.convertToDTO(evaluate,EvaluateDTO.class);
    }

    @Override
    public int saveEvaluate(Evaluate evaluate) {
        evaluate.setCreatedTime(new Date());
        evaluate.setIsDelete(Constants.EST_NOT);
        return evaluateDao.insert(evaluate);
    }

    @Override
    public EvaluateInfoDTO getEvaluateInfoById(Long id) {
        Evaluate evaluate = evaluateDao.selectByPrimaryKey(id);
        ModelDetailDTO modelDetailDTO =
                modelService.getById(evaluate.getModelId());
        List<EvaluateDetailDTO> evaluateDetailDTOS =
                evaluateDetailService.listByEvaluateId(evaluate.getId());

        EvaluateInfoDTO evaluateInfoDTO =
                BeanUtils.convertToDTO(evaluate, EvaluateInfoDTO.class);
        evaluateInfoDTO.setModel(modelDetailDTO);
        evaluateInfoDTO.setDetailList(evaluateDetailDTOS);

        return evaluateInfoDTO;
    }
}
